import {
  selectAll,
  add,
  allId,
  update,
  del,
  idToName
} from '@/api/patient'

const getDefaultState = () => {
  return {}
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  }
}

const actions = {
  // use select all
  selectAll({
    commit
  }, patientInfo) {
    return new Promise((resolve, reject) => {
      const {
        obj,
        page,
        pageSize
      } = patientInfo
      selectAll({
        obj: obj,
        page: page,
        pageSize: pageSize
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  allId({
    commit
  }, patientInfo) {
    const {
      name
    } = patientInfo
    return new Promise((resolve, reject) => {
      allId({
        name: name,
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  idToName({
    commit
  }, patientInfo) {
    const {
      id
    } = patientInfo
    return new Promise((resolve, reject) => {
      idToName({
        id: id
      }).then(response => {
        resolve(response.data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use add menu
  add({
    commit
  }, patientInfo) {
    return new Promise((resolve, reject) => {
      const {
        name,
        username,
        password,
        id
      } = patientInfo
      add({
        name: name,
        username: username,
        password: password,
        id: id
      }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use update
  update({
    commit
  }, patientInfo) {
    return new Promise((resolve, reject) => {
      const {
        patientId,
        name,
        username,
        password,
        id
      } = patientInfo
      update({
        patientId: patientId,
        name: name,
        username: username,
        password: password,
        id: id
      }).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // use delete 
  delete({
    commit
  }, ids) {
    return new Promise((resolve, reject) => {
      del(ids).then(response => {
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
